package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.entity.po.DataCenterReportInfoPo;
import com.xueyi.aixihu.entity.po.DtNewsSort;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.mapper.DataCenterMapper;
import com.xueyi.aixihu.pattern.FinanceReportStateStrategy;
import com.xueyi.aixihu.pattern.FinanceReportStateStrategyDirector;
import com.xueyi.aixihu.reqeust.FinanceReportReq;
import com.xueyi.aixihu.service.DataCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 数据中心业务实现类
 *
 * @author 刘梓江
 * @date 2023-12-12 12:00
 */
@Service
public class DataCenterServiceImpl implements DataCenterService {


    @Autowired
    private DataCenterMapper dataCenterMapper;

    @Override
    public ReportTotalNumberVo statisticsReportTotalNumber() {

        //获取报表审核状态策略对象
        FinanceReportStateStrategyDirector director =new FinanceReportStateStrategyDirector();
        FinanceReportStateStrategy underAuditStrategy = director.getFinanceReportUnderAuditStateStrategy();
        FinanceReportStateStrategy publicStrategy = director.getFinanceReportPublicStateStrategy();

        //村报表总数量
        int villageReportNumber=0;
        //社区报表总数量
        int communityReportNumber=0;
        //居委会报表总数量
        int committeeReportNumber=0;
        //审核数量、公开总数量
        int auditNumber=0;
        int publicNumber=0;

        //获取报表列表系你行
        List<DataCenterReportInfoPo> dataCenterReportInfoPos = dataCenterMapper.selectReportInfoList();
        for(int index=0;index<dataCenterReportInfoPos.size();index++){
            DataCenterReportInfoPo reportInfo = dataCenterReportInfoPos.get(index);

            //统计报表数量
            if(reportInfo.getAreaPid().equals(3)){

                //村
                villageReportNumber++;
            }else if(reportInfo.getAreaPid().equals(16)){

                //社区
                communityReportNumber++;
            }else{

                //居委
                committeeReportNumber++;
            }

            //统计报表状态(审核中，公开)数量
            if(underAuditStrategy.support(reportInfo.getCurrentStatus().toString())){
                auditNumber++;
            }else if(reportInfo.getIsPublic().equals(1)){
                publicNumber++;
            }
        }

        return ReportTotalNumberVo.builder()
                .villageReportNumber(villageReportNumber)
                .communityReportNumber(communityReportNumber)
                .committeeReportNumber(committeeReportNumber)
                .auditNumber(auditNumber)
                .publicNumber(publicNumber).build();
    }

    @Override
    public AreaReportAuditStateInfoVo selectAreaReportAuditInfoList(SelectAreaReportAuditReq reportAuditReq) {
        AreaReportAuditStateInfoVo areaReportAuditStateInfoVo=new AreaReportAuditStateInfoVo();

        //查询区域列表信息
        List<DtNewsSort> dtNewsSorts = dataCenterMapper.selectNewsSortByPid(reportAuditReq.getAreaType());
        if(!CollectionUtils.isEmpty(dtNewsSorts)){

            //获取报表审核状态策略对象
            FinanceReportStateStrategyDirector director =new FinanceReportStateStrategyDirector();
            FinanceReportStateStrategy waitSubmitStrategy = director.getFinanceReportWaitSubmitStateStrategy();

            //封装区域信息
            Map<Integer,AreaReportAuditStateVo> areaMap=new HashMap<>();
            dtNewsSorts.forEach(
                e->{
                    areaMap.put(e.getId(),new AreaReportAuditStateVo(e.getName(),waitSubmitStrategy.getColor(),waitSubmitStrategy.getStateLevel()));
                }
            );

            //获取报表列表信息
            List<DataCenterReportInfoPo> dataCenterReportInfoPos = dataCenterMapper.selectReportInfoListByAreaAndMonth(reportAuditReq.getAreaType(), reportAuditReq.getMonth());
            dataCenterReportInfoPos.forEach(
                    e->{
                        AreaReportAuditStateVo areaReportAuditStateVo = areaMap.get(e.getFromToAreaId());
                        //获取当前报表的审核状态策略对象
                        FinanceReportStateStrategy strategy;
                        if (e.getIsPublic().equals(1)) {
                            strategy = director.getFinanceReportPublicStateStrategy();
                        }else{
                            strategy = director.getStateStrategy(e.getCurrentStatus().toString());
                        }
                        if(strategy.getStateLevel()>areaReportAuditStateVo.getAuditStateLevel()){
                            //设置当前区域报表审核状态等级信息（作用：页面只显示审核状态等级最大的情况）
                            areaReportAuditStateVo.setAuditStateLevel(strategy.getStateLevel());
                            areaReportAuditStateVo.setAuditStateColor(strategy.getColor());
                        }
                    }
            );
            //统计区域审核状态信息
            areaMap.forEach(
                    (key,value)->{
                        switch (value.getAuditStateLevel()){
                            case 1:areaReportAuditStateInfoVo.setWaitSubmitNumber(areaReportAuditStateInfoVo.getWaitSubmitNumber()+1); break;
                            case 2:areaReportAuditStateInfoVo.setUnderAuditNumber(areaReportAuditStateInfoVo.getUnderAuditNumber()+1); break;
                            case 3:areaReportAuditStateInfoVo.setAuditPassedNumber(areaReportAuditStateInfoVo.getAuditPassedNumber()+1); break;
                            case 4:areaReportAuditStateInfoVo.setPublicNumber(areaReportAuditStateInfoVo.getPublicNumber()+1); break;
                        }

                    }
            );
            areaReportAuditStateInfoVo.setAreaReportAuditStates(new ArrayList<>(areaMap.values()));
        }
        return areaReportAuditStateInfoVo ;
    }

    @Override
    public List<AxhAuditFinanceLogVo> selectUserOperationReportLogList(FinanceReportReq reportReq) {
        reportReq.countStartIndex();
        List<AxhAuditFinanceLogVo> axhAuditFinanceLogVos = dataCenterMapper.selectUserOperationReportLogList(reportReq.getStartIndex(), reportReq.getLimit());
        return axhAuditFinanceLogVos;
    }
}
